package com.tianji.learning.task;

import com.tianji.common.utils.CollUtils;
import com.tianji.learning.domain.po.LearningLesson;
import com.tianji.learning.enums.LessonStatus;
import com.tianji.learning.service.ILearningLessonService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class LessonExpireTask {

    private final ILearningLessonService iLearningLessonService;

    @Scheduled(cron = "0 */1 * * * ?")//每个一分钟执行一次
    public void executeTask(){
        log.info("定时查询课表是否过期开始执行：{}",LocalDateTime.now());
        List<LearningLesson> list = iLearningLessonService.lambdaQuery()
                .lt(LearningLesson::getExpireTime,LocalDateTime.now())
                .ne(LearningLesson::getStatus,LessonStatus.EXPIRED)
                .list();

        if(CollUtils.isNotEmpty(list)){
            for (LearningLesson lesson : list) {
                lesson.setStatus(LessonStatus.EXPIRED);
            }
            boolean success = iLearningLessonService.updateBatchById(list);
            if(!success){
                log.error("修改过期课程失败");
            }
        }
    }
}
